package com.fh.shop.api.member.controller;

import com.fh.shop.api.annotation.NeedLogin;
import com.fh.shop.api.common.ServerResponse;
import com.fh.shop.api.common.SystemConst;
import com.fh.shop.api.member.biz.IMemberService;
import com.fh.shop.api.member.param.EditPasswordParam;
import com.fh.shop.api.member.param.LoginParam;
import com.fh.shop.api.member.param.MemberParam;
import com.fh.shop.api.member.vo.MemberVo;
import com.fh.shop.api.util.KeyUtil;
import com.fh.shop.api.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/api/members")
@Api(tags = "会员接口", description = "电商前台的会员接口")
public class MemberController {
    @Resource(name = "memberService")
    private IMemberService memberService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private HttpServletResponse response;
    @Value("${mail.active.member.success}")
    private String mailSuccess;
    @Value("${mail.active.member.error}")
    private String mailError;

    @PostMapping
    public ServerResponse reg(@RequestBody MemberParam memberParam) {
        return memberService.reg(memberParam);
    }

    @GetMapping("/active/{x}")
    public void activeMember(@PathVariable("x") String uuid) throws IOException {
        int status = memberService.activeMember(uuid);
        if (status == SystemConst.MEMBER_ACTIVE_OK) {
            response.sendRedirect(mailSuccess);
        } else {
            response.sendRedirect(mailError);
        }
    }

    @GetMapping("/mail/active/{x}")
    public void activeMailMember(@PathVariable("x") String uuid) throws IOException {
        boolean success = memberService.activeMailMember(uuid);
        if (success) {
            response.sendRedirect(mailSuccess);
        } else {
            response.sendRedirect(mailError);
        }
    }

    @PostMapping("/sendActiveMail/{mail}")
    // 路径传参，就不需要做 非空验证了
    public ServerResponse sendActiveMail(@PathVariable String mail) {
        return memberService.sendActiveMail(mail);
    }

    @PostMapping("/login")
    @ApiOperation("登录")
    public ServerResponse login(LoginParam loginParam) {
        return memberService.login(loginParam);
    }

    @GetMapping("/findMember")
    @NeedLogin
    @ApiOperation("获取会员信息")
    @ApiImplicitParam(name = "x-auth", value = "头信息", paramType = "header")
    public ServerResponse findMember() {
        MemberVo memberVo = (MemberVo) request.getAttribute(SystemConst.CURR_MEMBER);
        String nickName = memberVo.getNickName();
        return ServerResponse.success(nickName);
    }

    @PostMapping("/logout")
    @NeedLogin
    public ServerResponse logout() {
        MemberVo memberVo = (MemberVo) request.getAttribute(SystemConst.CURR_MEMBER);
        Long id = memberVo.getId();
        // 清空数据
        RedisUtil.delete(KeyUtil.buildMemberKey(id));
        return ServerResponse.success();
    }

    @PostMapping("/editPassword")
    @NeedLogin
    public ServerResponse editPassword(EditPasswordParam passwordParam) {
        MemberVo memberVo = (MemberVo) request.getAttribute(SystemConst.CURR_MEMBER);
        Long id = memberVo.getId();
        passwordParam.setMemberId(id);
        return memberService.updatePassword(passwordParam);
    }
}
